<template>
  <div>
    <van-field v-model="name" label="名字" placeholder="名字" />
    <van-field v-model="mobile" label="手机号" placeholder="手机号" />
    <van-field
      v-model="password"
      type="password"
      label="密码"
      placeholder="请输入密码"
    />
    <van-field v-model="code" label="验证码" placeholder="验证码" /><van-button
      type="primary"
      @click="submit"
      >获取验证码</van-button
    >
    <br /><br />

    <van-button type="primary" @click="register">注册</van-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      name: "",
      mobile: "",
      password: "",
      code: "",
    };
  },
  methods: {
    submit:function(){
      if (this.name == "") {
        this.$toast.fail("用户名不能为空");
        return false;
      }
      if (this.password == "") {
        this.$toast.fail("密码不能为空");
        return false;
      }
      if (this.mobile == "") {
        this.$toast.fail("手机号不能为空");
        return false;
      }
      this.myaxios(this.host + 'users/smscode/', 'post',{
        mobile:this.mobile
      }).then((data) => {
        console.log(data);
        if (data.errcode ==0 ){
          this.$toast.success(data.msg)
        }else{
          this.$toast.fail(data.msg)
        }
      })
    },
    register: function () {
      this.myaxios(this.host + "users/register/", "post", {
        nickname: this.name,
        mobile: this.mobile,
        password: this.password,
        code: this.code,
      }).then((data) => {
        console.log(data);
        if (data.errcode == 0) {
          this.$toast.success(data.msg);
          this.$router.push("/")
        } else {
          this.$toast.fail(data.msg);
        }
      });
    },
  },
};
</script>

<style>
</style>